package com.fe.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fe.mapper.DealMapper;
import com.fe.mapper.PatriarchMapper;
import com.fe.mapper.TeacherMapper;
import com.fe.model.Deal;
import com.fe.model.Patriarch;
import com.fe.model.Teacher;
import com.fe.service.DealService;
@Service
public class DealServiceImpl implements DealService{
	
	@Autowired
	private DealMapper dealMapper;
	@Autowired
	private PatriarchMapper patriarchMapper;
	@Autowired
	private TeacherMapper teacherMapper;
	
	/**
	 * 
	 * 方法描述：创建订单
	 * @param request
	 * @param map
	 * @param tea_id
	 * @param deal_time
	 * @author ssh
	 * @date 2018年8月25日 下午2:52:21
	 */
	@Override
	public void addDeal(HttpServletRequest request, Map<String, Object> map, Deal deal) {
		//获取家长id，并装入
		HttpSession session = request.getSession();
		Patriarch patriarch = (Patriarch) session.getAttribute("patriarch");
		
		//判断余额是否足够
		int pat_balance = patriarch.getPat_balance();
		if (pat_balance < Integer.parseInt(deal.getDeal_money())) {
			throw new RuntimeException("余额不足！");
		}
		
		//从家长账户中扣除余额
		patriarch.setPat_balance(patriarch.getPat_balance()-Integer.parseInt(deal.getDeal_money()));
		patriarchMapper.update(patriarch);
		session.setAttribute("patriarch", patriarchMapper.load(patriarch.getPat_id()));
		
		//装入将家长id
		int pat_id = patriarch.getPat_id();
		deal.setPat_id(pat_id);
		
		//装入订单时间
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		deal.setDeal_date(simpleDateFormat.format(date));
		//添加订单
		int result = dealMapper.add(deal);
		if (result < 0) {
			throw new RuntimeException("提交失败！");
		}
		map.put("message", "提交成功！");
	}
	
	/**
	 * 
	 * 方法描述：根据id获取所有订单
	 * @param map
	 * @param request
	 * @author ssh
	 * @date 2018年8月25日 下午7:22:38
	 */
	@Override
	public void listDeal(Map<String, Object> map) { 
		int count = dealMapper.count(map);
		if (count == 0) {
			throw new RuntimeException("暂无数据!");
		}
		map.put("count", count);
		List<Deal> listDeal = dealMapper.listDeal(map);
		if (listDeal == null) {
			throw new RuntimeException("暂无数据!");
		}
		map.put("listDeal", listDeal);
	}
	
	/**
	 * 
	 * 方法描述：更新订单中的评论，状态
	 * @param deal
	 * @author ssh
	 * @date 2018年8月26日 下午2:14:15
	 */
	@Override
	public void updateDeal(Deal deal) {
		int result = dealMapper.update(deal);
		if (result < 0) {
			throw new RuntimeException("更新失败!");
		}
		//如果是2，即家长确认已经完成,更新家教的余额
		if (Objects.equals(deal.getDeal_status(), "2")) {
			Deal deal2 = dealMapper.load(deal.getId());
			int tea_id = deal2.getTea_id();
			Teacher teacher = teacherMapper.loadTeacher(tea_id);
			teacher.setTea_balance(teacher.getTea_balance()+Integer.parseInt(deal2.getDeal_money()));
			int result2 =  teacherMapper.update(teacher);
			if (result2 < 0) {
				throw new RuntimeException("更新失败!");
			}
		}
	}

	/**
	 * 
	 * 方法描述：查询一个家教的评论，按时间逆序排列
	 * @param map
	 * @param tea_id
	 * @author ssh
	 * @date 2018年8月26日 下午7:40:57
	 */
	@Override
	public void listDealByTeaId(Map<String, Object> map, Integer tea_id) {
		map.put("tea_id", tea_id);
		int count = dealMapper.countDealByTeaId(tea_id);
		if (count == 0) {
			throw new RuntimeException("暂无数据!");
		}
		map.put("count", count);
		List<Deal> listDealByTeaId = dealMapper.listDealByTeaId(map);
		if (listDealByTeaId == null) {
			throw new RuntimeException("暂无数据!");
		}
		map.put("listDealByTeaId", listDealByTeaId);
	}
	
	

}
